<template>
  <view class="my-settle">
    <label class="radio" @click="changeGoodsState">
      <radio :checked="isFullChecked" color="#c00000"/><text>全选</text>
    </label>
    <view class="price">
      合计：<text class="total-price">￥{{checkedToatlPeice}}</text>
    </view>
    <view class="btn-settle" @click="settlement">结算({{checkedCount}})</view>
  </view>
</template>

<script>
  import {mapGetters,mapMutations,mapState} from 'vuex'
  export default {
    name:"my-settle",
    data() {
      return {
        seconds:3,
        timer:null
      };
    },
    computed:{
      ...mapGetters('cart',['checkedCount','total','checkedToatlPeice']),
      ...mapGetters('user',['addstr']),
      ...mapState('user',['token']),
      ...mapState('cart',['cart']),
      isFullChecked(){
        return this.checkedCount === this.total
      }
    },
    methods:{
      ...mapMutations('cart',['updateAllGoodsState']),
      ...mapMutations('user',['updateRedirectInfo']),
      changeGoodsState(){
        this.updateAllGoodsState(!this.isFullChecked)
      },
      settlement(){
        if(!this.checkedCount) return uni.$showMsg('请选择要结算的商品！')
        if(!this.addstr) return uni.$showMsg('请选择收货地址！')
        if(!this.token) return this.delay()
        this.payOrder()
      },
      async payOrder(){
        const orderInfo = {
          order_price:0.01,
          consignee_addr:this.addstr,
          goods:this.cart.filter(x => x.goods_state).map(x =>({
            goods_id:x.goods_id,
            goods_number:x.goods_count,
            goods_price:x.goods_price
          }))
        }
        const {data:res} = await uni.$http.post('/api/public/v1/my/orders/create',orderInfo)
        if(res.meta.status !== 200) return uni.$showMsg('订单创建失败！')
        const orderNumber = res.message.order_number
        const {data:res1} = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder',{order_number:orderNumber})
        if(res1.meta.status !== 200) return uni.$showMsg('预支付订单生成失败！')
        const payInfo = res1.message.pay
        const [err,succ] = await uni.requestPayment(payInfo)
        if(err) return uni.$showMsg('订单未支付！')
        const {data:res2} = await uni.$http.post('/api/public/v1/my/orders/chkOrder',{order_number:orderNumber})
        console.log(res2)
        if(res2.meta.status !== 200) return uni.$showMsg('订单未支付！')
        uni.showToast({
          icon:'success',
          title:'支付完成！'
        })
      },
      showTips(n){
        uni.showToast({
          title: n+'秒后跳转至登录',
          icon:'none',
          mask:true,
          duration: 1500
        })
      },
      delay(){
        this.seconds = 3
        this.showTips(this.seconds)
        this.timer = setInterval(()=>{
          this.seconds--
          if(this.seconds <= 0){
            clearInterval(this.timer)
            uni.switchTab({
              url:'/pages/my/my',
              success: () => {
                this.updateRedirectInfo({
                  openType:'switchTab',
                  from:'/pages/cart/cart'
                })
              }
            })
            return
          }
          this.showTips(this.seconds)
        },1000)
      }
    }
  }
</script>

<style lang="scss">
.my-settle{
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-sizing: border-box;
  padding: 0 10px;
  font-size: 14px;
  .radio{
    display: flex;
    align-items: center;
  }
  .total-price{
    color: #c00000;
  }
  .btn-settle{
    background-color: #c00000;
    width: 100px;
    height: 100%;
    color: #fff;
    line-height: 50px;
    text-align: center;
  }
}
</style>